//委托产品管理 显示表格及js实体

Ext.define('AgentProduct', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
        , "AgentId"   //委托单位Id
        , "AgentName"   //委托单位名称
        , "City"   //可覆盖城市
        , "CityCode"   //城市编号
        , "Product"   //可提供产品
        , "ProductId"   //产品Id
        , "Price"   //委托价格
        , "IsChecked" //是否已委托
    ],
    idProperty: "Id"
});

Ext.define('SHApp.CompanyManage.AgentProduct.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.companymanage.agentproduct.grid',
    // hasPageBar: false,    //是否有分页工具栏  
    isSelModel: false,
    firstLoad: true,
    hasEditCell: true,
    autoSync: false,
    pvalue: 1536,  //用户权限值
    txtAreaNameID: "_txtAreaNameID",
    agentId: "",
    agentName: "",
    city: "",
    dataModel: "AgentProduct",
    dataUrl: "api/agentproduct2?format=json",
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnSaveID += me.id;
        me.btnResetID += me.id;
        me.txtAreaNameID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    loadDataByCity: function (agentId, agentName, city) {
        this.city = city;
        this.dataUrl = "api/agentproduct2?format=json&AgentId=" + agentId + "&AgentName=" + agentName + "&CityCode=" + city;
        this.reLoadData();
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "可提供产品",
            width: 200,
            dataIndex: 'Product',
            sortable: true
        }, {
            text: "产品Id",
            width: 100,
            dataIndex: 'ProductId',
            sortable: true,
            hidden: true
        }, {
            xtype: 'checkcolumn',
            text: '是否委托',
            dataIndex: 'IsChecked',
            width: 100,
            stopSelection: false,
            align: "center"
        }, {
            text: "委托价格",
            flex: 1,
            dataIndex: 'Price',
            sortable: false,
            renderer: function (v) {
                if (v == 0) {
                    return '';
                } else {
                    return new Number(v).toFixed(2);
                }
            },
            editor: {
                xtype: "numberfield",
                minValue: 0,
                maxValue: 1.79769e+308
            }
        }
		]
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
            '->', {
                xtpye: "button",
                text: "保存",
                id: me.btnSaveID,
                iconCls: "icon-btn-save",
                handler: function () {
                    me.saveData();
                }
            }, ' ', {
                xtpye: "button",
                text: "重置",
                id: me.btnResetID,
                iconCls: "icon-btn-reset",
                handler: function () {
                    me.reSetData();
                }
            }]
    },

    // 保存
    saveData: function () {
        var me = this;
        var store = me.store;
        var jsData = [];
        for (var i = 0, len = store.data.length; i < len; i++) {

            var id = store.data.items[i].data.Id;
            var productName = store.data.items[i].data.Product;
            var productId = store.data.items[i].data.ProductId;
            var price = store.data.items[i].data.Price;
            var isChecked = store.data.items[i].data.IsChecked;

            var obj = new Object();
            obj.Id = id;
            obj.AgentId = me.agentId;
            obj.AgentName = me.agentName;
            obj.City = me.down("#" + me.txtAreaNameID).getDisplayValue();
            obj.CityCode = me.down("#" + me.txtAreaNameID).getValue();
            obj.Product = productName;
            obj.ProductId = productId;
            obj.Price = price;
            obj.IsChecked = isChecked;
            jsData.push(obj);
        }
        Ext.Ajax.request({
            url: "api/agentproductList?format=json",
            method: "POST",
            jsonData: jsData,
            success: function (response, opts) {
                store.load();
                Ext.Msg.alert("提示", "保存成功！");
            },
            failure: function (response, opts) {
                Ext.Msg.alert("错误", "状态：" + response.status + " - " + response.statusText);
            }
        });
    },
    // 重置
    reSetData: function () {
        this.store.load();
    }
});

